{extend name="base"/}
{block name="resources"}
<style>
  .form-wrap {margin-top: 0;}
  .form-row .layui-btn{margin-right:14px;}
  .layui-btn+.layui-btn{margin-left: 0;}
</style>
{/block}
{block name="main"}
<div class="layui-form form-wrap">
  <div class="layui-form-item">
    <label class="layui-form-label"><span class="required">*</span>社群名称：</label>
    <div class="layui-input-block">
      <input name="qr_name" type="text" lay-verify="qr_name" lay-verify="required" value="{$qr_data.qr_name}" class="layui-input len-long" autocomplete="off">
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label"><span class="required">*</span>社群描述：</label>
    <div class="layui-input-block">
      <textarea name="community_describe" class="layui-textarea len-long" maxlength="100" lay-verify="community_describe" id="" lay-verify="required"  cols="30" rows="10">{$qr_data.community_describe}</textarea>
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label img-upload-lable short-label"><span class="required">*</span>二维码图片：</label>
    <div class="layui-input-inline">
      <div class="upload-img-block square">
        <div class="upload-img-box {notempty name="$qr_data['qr_img']"}hover{/notempty}">
          <div class="upload-default" id="qr_img">
            {if condition="$qr_data['qr_img']"}
              <div class="preview_img" id="ceshi">
                <img src="{:img($qr_data['qr_img'])}" class="img_prev" data-id="qr_img" layer-src/>
              </div>
            {else/}
              <div class="upload">
                <i class="iconfont iconshangchuan"></i>
                <p>点击上传</p>
              </div>
            {/if}
          </div>
          <div class="operation">
            <div>
              <i title="图片预览" class="iconfont iconreview js-preview"></i>
              <i title="删除图片" class="layui-icon layui-icon-delete js-delete"></i>
            </div>
            <div class="replace_img js-replace">点击替换</div>
          </div>
          <input type="hidden" name="qr_img" lay-verify="qr_img" value="{$qr_data['qr_img']}"/>
        </div>
      </div>
    </div>
  </div>
  <div class="form-row">
    <input type="hidden" name="qr_id" value="{$qr_data['qr_id']}"/>
    <button id="submit" class="layui-btn" lay-submit lay-filter="save">提交</button>
    <button class="layui-btn layui-btn-primary" onclick="back()">返回</button>
  </div>
  
</div>
{/block}
{block name="script"}
<script>
  layui.use(['form', 'laydate'], function() {
    var form = layui.form,
	laydate = layui.laydate,
	repeat_flag = false; //防重复标识
			
    form.render();

    laydate.render({
      elem: '#laydate'
    });

    var upload = new Upload({
		elem: '#qr_img',
		callback:function(res){
			// if(res.code==10067){
			// 	$('#submit').css('display','inline-block')
			// }
		}
    });
	
    /**
     * 表单验证
     */
	
    form.verify({
      qr_name: function(value){
        if (!/[\S]+/.test(value)) {
          return '请输入二维码名称';
        }
      },
      community_describe: function(value){
        if (!/[\S]+/.test(value)) {
          return '请输入社群描述';
        }
      },
      qr_img: function(value){
        if (!/[\S]+/.test(value)) {
          return '请上传社群二维码';
        }
      }
    });

    /**
     * 监听提交
     */
    form.on('submit(save)', function(data) {
      // 删除图片
      if(!data.field.qr_img) upload.delete();

      if(repeat_flag) return false;
      repeat_flag = true;
	  
      $.ajax({
        url: ns.url("shop/goods/editqrcode"),
        data: data.field,
        dataType: 'JSON', //服务器返回json格式数据
        type: 'POST', //HTTP请求类型
        success: function(res) {
          repeat_flag = false;
          if (res.code == 0) {
            layer.confirm('编辑成功', {
              title:'操作提示',
              btn: ['返回列表'],
              closeBtn: 0,
              yes: function(){
                location.href = ns.url("shop/goods/communityqrcode")
              },
            });
          }else{
            layer.msg(res.message);
          }
        }
      });
    });

  });

  function back() {
    location.href = ns.url("shop/goods/communityqrcode");
  }
</script>
{/block}